package com.spring_mysql_redis.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spring_mysql_redis.annotation.CurrentUser;
import com.spring_mysql_redis.entity.Comment;
import com.spring_mysql_redis.entity.DynamicComm;
import com.spring_mysql_redis.entity.UserDynamic;
import com.spring_mysql_redis.entity.Users;
import com.spring_mysql_redis.model.ResultFormat;
import com.spring_mysql_redis.service.CommentService;
import com.spring_mysql_redis.utils.ResultUtil;
import com.spring_mysql_redis.utils.TimeUtitl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 */
@Api(tags = "动态评论管理")
@RestController
@RequestMapping("/comment")
public class CommentController {

    private Logger logger = LoggerFactory.getLogger(CommentController.class);

    @Autowired
    private CommentService commentService;

    /**
     * 全查询评论信息
     *
     * @return
     */
    @ApiOperation("comment+user联合查询全部动态接口")
    @GetMapping("/lists")
    public Page<DynamicComm> pgDynamicCommList(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit,@RequestParam("dynamicId") Integer dynamicId) {
        logger.info("-------------------进入用户查询controller层--------------------------");
        Page<DynamicComm> iPage = new Page<DynamicComm>(page, limit);
        return commentService.selectDynamicCommList(iPage,dynamicId);

    }

    /**
     * 为动态添加评论
     *
     * @param comment
     * @return
     */
    @ApiOperation("为动态添加评论接口")
    @PostMapping("/save")
    @ResponseBody
    public ResultFormat savePgSorts(@RequestBody Comment comment, @CurrentUser Users user) {
        comment.setCreateTime(TimeUtitl.dateTime());
        comment.setUserId(user.getId());
        ResultFormat insert = commentService.saveComment(comment);

        if (insert.getCode() == 200) {
            logger.info("添加成功");
            return ResultUtil.success();
        }
        if (insert.getCode() == 100) {
            logger.info("用户未登录");
            return ResultUtil.error(insert.getCode(), insert.getMsg());
        }
        if (insert.getCode() == 101) {
            logger.info("未填写内容");
            return ResultUtil.error(insert.getCode(), insert.getMsg());
        }

        return ResultUtil.error(102, "添加失败");
    }

    @ApiOperation("查询评论接口")
    @GetMapping("/select_list")
    @ResponseBody
    public ResultFormat selectListPgSorts(Integer dynamicId) {

        List comments = commentService.selectCommentList(dynamicId);

        if (comments != null){
            logger.info("查询成功");
            return ResultUtil.success();
        }

        return ResultUtil.error(100, "添加失败");
    }

    @ApiOperation("删除评论接口")
    @PutMapping("/delete")
    @ResponseBody
    public ResultFormat deleteListPgSorts(Comment comment) {

        ResultFormat del = commentService.deleteComment(comment);

        if (del.getCode() == 200){
            logger.info("删除成功");
            return ResultUtil.success();
        }

        return ResultUtil.error(100, "删除失败");
    }
}
